#include "ObBrick.h"
#include "D3DApp.h"
#include "Collision.h"
#include "ObPlayer.h"

IDirect3DTexture9* ObBrick::mTex = 0;
CSound* ObBrick::sBreak = 0;
ObBrick::ObBrick(){
	setId(BRICK1_BR);
	mInfo	= 0;
}

ObBrick::ObBrick(BRICK nid){
	setId(nid);
	mInfo	= 0;
}

ObBrick::~ObBrick(){

}

bool ObBrick::setId(int nid){
	if(checkType(nid)){
		mID = nid;
		mWidth	= 16.0f;
		mHeight = 16.0f;
		mCenter = D3DXVECTOR3(mWidth/2, mHeight/2, 0.0f);
		mCankill = false;
		jumpcount = 0.0f;
		delaycount = 0.0f;
		cankilldelay = 0.0f;
		//cal brick spritepos
		mSpritePos.x = 8;
		mSpritePos.y = 8;
		int x,y;
		y = (mID-1)/3;
		x = (mID-1)%3;
		mSpritePos.x +=x*16;
		mSpritePos.y +=y*16;
		return true;
	}else{
		return false;
	}
}

bool ObBrick::checkType(int obID){
	if(obID<BRICK1_BR || obID>BRICK3_NBR){
		return false;
	}else{
		return true;
	}
}

bool ObBrick::getCanKill(){
	return mCankill;
}
void ObBrick::setCanKill(bool cankill){
	mCankill = cankill;
}

void ObBrick::collidedWith(ObObject* object, CollisionRS rs){
	switch (object->getId())
	{
	case ObPlayer::MARIO:
		if(ObPlayer* player  = dynamic_cast<ObPlayer*>(object)){
			if(rs.mState == Bottom && (mID == BRICK1_BR ||mID == BRICK2_BR ||mID == BRICK3_BR)){
				mCankill = true;
				cankilldelay = 0.05f;
				if(player->getStatus() == SMALL){
					if(jumpcount == 0.0f){
						mOldPos.x = mPos.x;
						mOldPos.y = mPos.y;
						jumpcount = 0.2f;
					}
					
				}else{
					PlaySound(sBreak);
					RECT rect = calRenderRECT(mSpritePos,mWidth,mHeight);
					BrickKBoom* br1 = new BrickKBoom(mTex,rect,mPos.x,mPos.y + 8.0f,50,300,0.5f);
					BrickKBoom* br2 = new BrickKBoom(mTex,rect,mPos.x,mPos.y + 8.0f,70,200,0.45f);
					BrickKBoom* br3 = new BrickKBoom(mTex,rect,mPos.x,mPos.y + 8.0f,-50,300,0.5f);
					BrickKBoom* br4 = new BrickKBoom(mTex,rect,mPos.x,mPos.y + 8.0f,-70,200,0.4f);

					mBoomlist.push_back(br1);
					mBoomlist.push_back(br2);
					mBoomlist.push_back(br3);
					mBoomlist.push_back(br4);
					delaycount = 1.0f;
					
				}
			}
		}
		break;
	default:
		break;
	}
}

void ObBrick::update(float dt){
	if(mNeedDispose) return;

	if(mCankill){
		cankilldelay -=dt;
		if(cankilldelay<=0.0f){
			cankilldelay = 0.0f;
			mCankill = false;
		}
	}
	if(jumpcount> 0.0f){
		jumpcount-=dt;
		if(jumpcount > 0.1f){
			mPos.y +=100*dt;
		}else if(jumpcount > 0.0f){
			mPos.y -=100*dt;
		}else{
			jumpcount = 0.0f;
			mPos.y = mOldPos.y;
		}
	}

	if(delaycount>0.0f){
		delaycount -=dt;
		if(delaycount< 0.95f){
			mNeedCollision = false;
		}
		if(delaycount<=0.0f){
			delaycount = 0.0f;
			for(std::vector<BrickKBoom*>::size_type i = 0; i < mBoomlist.size();i++){
				mBoomlist[i]->clear();
				delete mBoomlist[i];
			}
			mBoomlist.clear();
			mNeedDispose = true;
		}

		for(std::vector<BrickKBoom*>::size_type i = 0; i < mBoomlist.size();i++){
			mBoomlist[i]->update(dt);
		}
	}
}

void ObBrick::render(ID3DXSprite* mSprite){
	if(mID == 0) return;
	if(mTex == 0) return;
	if(mNeedDispose) return;
	
	for(std::vector<BrickKBoom*>::size_type i = 0; i < mBoomlist.size();i++){
		mBoomlist[i]->render(mSprite);
	}

	if(mNeedCollision == false) return;
	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,true));
	D3DXMATRIX S;
	D3DXMatrixScaling(&S,1.0f,-1.0f,1.0f);
	D3DXMATRIX R;
	D3DXMatrixRotationZ(&R, mRotation);
	D3DXMATRIX T;
	D3DXMatrixTranslation(&T,mPos.x - gCamera->getPos().x,mPos.y - gCamera->getPos().y,0);
	mSprite->SetTransform(&(S*R*T));
	RECT rect = calRenderRECT(mSpritePos,mWidth,mHeight);
	mSprite->Draw(mTex,&rect,&mCenter,0,D3DCOLOR_XRGB(255, 255, 255));
	mSprite->Flush();
	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,false));
}

bool ObBrick::loadTexture(){
	if(mTex == 0){
		HR(D3DXCreateTextureFromFileEx(
			gDevice,
			"Sprites/Brick.png",
			D3DX_DEFAULT_NONPOW2,
			D3DX_DEFAULT_NONPOW2,
			0,
			0,
			D3DFMT_UNKNOWN,
			D3DPOOL_MANAGED,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_XRGB(0,0,0),
			NULL,
			NULL,
			&mTex));
	}

	sBreak = LoadSound("Sounds\\BrickBroken.wav");
	return true;
}

bool ObBrick::releaseTexture(){

	if(mTex != 0){
		ReleaseCOM(mTex);
		
	}

	return true;
}



